package rov.ui.widget;


import rov.data.SensorData;
import rov.util.Utils;

import java.awt.*;
import javax.swing.*;


// graphical swing component used for displaying temperatures
public class PodPanel extends JPanel
{
    private String mName;
    private SensorData mSensor;
    private int mTempsIndex;
    private int[] mPercents;


    // name       - displayed title
    // sensor     - data source
    // tempsIndex - index into sensor to get data from
    public PodPanel(String name, SensorData sensor, int tempsIndex)
    {
        mName = name;
        mSensor = sensor;
        mTempsIndex = tempsIndex;
        mPercents = new int[] { 0, 33, 66, 100 };

        setBackground(Color.white);
        //setBackground(Color.black);
        setPreferredSize(new Dimension(150, 225));
    }


    // returns the color to display for a given temperature in celsius
    private static Color celsiusToColor(double tempC)
    {
       	// 0 deg fah and below is blue
        if (tempC <= -18)
            return Color.blue;

        // 176 deg fah and above is red
        if (tempC >= 80)
            return Color.red;

        final int whiteTempC = 15;

        // 0 deg fah is blue
     	// 60 deg fah is white
        if (tempC <= whiteTempC)
        {
            float amt = (float) ((tempC + 18) / 33.0);
            return new Color(amt, amt, 1);
     	}

        // 60 deg fah is white
        // 176 deg fah is red
        float amt = (float) (1.0 - (tempC - whiteTempC) / 65.0);
        return new Color(1, amt, amt);
    }


    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g;
        FontMetrics fm = g2.getFontMetrics();
        int podTop = fm.getHeight() + 5;
        int podWidth = getWidth() - 10;
        int podHeight = getHeight() - podTop - 5;

        for (int i = 0; i < mPercents.length - 1; i++)
        {
            int y1 = (int) (mPercents[i] / 100.0 * podHeight) + podTop;
            int y2 = (int) (mPercents[i+1] / 100.0 * podHeight) + podTop;

            Color c1 = celsiusToColor(mSensor.getValue(mTempsIndex+i));
            Color c2 = celsiusToColor(mSensor.getValue(mTempsIndex+i+1));

            if (!c1.equals(c2) && (c1.getRed() == c2.getBlue() || c1.getBlue() == c2.getRed()))
                g2.setPaint(new LinearGradientPaint(0, y1, 0, y2, new float[] { 0, .5f, 1 }, new Color[] { c1, Color.white, c2 }));
            else
                g2.setPaint(new GradientPaint(0, y1, c1, 0, y2, c2));

            g2.fillRect(5, y1, podWidth, y2 - y1);
        }

        g2.setColor(Color.black);
        //g2.setColor(Color.white);
        g2.drawRect(5, podTop, podWidth, podHeight);
        g2.drawString(mName, getWidth() / 2 - fm.stringWidth(mName) / 2, fm.getHeight());
        //g2.setColor(Color.black);

        for (int i = 0; i < mPercents.length; i++)
        {
            double tempC = (int) (mSensor.getValue(i+mTempsIndex) * 100.0) / 100.0;
            double tempF = (int) (Utils.celsiusToFahrenheit(mSensor.getValue(i+mTempsIndex)) * 100.0) / 100.0;

            String tempCStr = tempC + "";
            String tempFStr = tempF + "";

            if (tempC < 10)
                tempCStr = " " + tempCStr;

            while (tempCStr.length() < 5)
                tempCStr += "0";

            if (tempF < 10)
                tempFStr = " " + tempFStr;

            while (tempFStr.length() < 5)
                tempFStr += "0";

            String tempStr = tempCStr + "" + (char) 0xB0 + "C (" + tempFStr + (char) 0xB0 + "F)";
            int y = (int) (mPercents[i] / 100.0 * (podHeight - fm.getHeight() * 2)) + podTop + fm.getHeight() + 5;

            g2.drawString(tempStr, getWidth() / 2 - fm.stringWidth(tempStr) / 2, y);
        }
    }
}
